﻿#include <sstream>  
#include <Windows.h>  
#include <hge.h>
#include <hgeresource.h>

HGE* hge = NULL;

//独一无二的类名，一般用GUID字串，以免与其他程序的类名重复  
static const wchar_t * CLASS_NAME = L"{DB0466A0-2230-78D3-87BA-166745B8DFBF}";  
static const wchar_t * CHILD_CLASS = L"{BA98C3D9-37A7-D997-A2BC-FB26C78DC089}";  
  HWND main_wnd;
  HWND child_wnd;
bool registerChildClass();
/************************************************************************ 
    回调函数，当主窗口收到任何Windows消息时被调用 
************************************************************************/  
LRESULT CALLBACK onMainWndMessage(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam) {  
    switch (msg) {  
    case WM_DESTROY:  
		hge->System_Shutdown();
		hge->Release();
		PostQuitMessage(0); //如果是“窗口销毁”事件，则应该在消息队列中投递  
        break;              //一个WM_QUIT消息，使GetMessage()返回FALSE  
    
	case WM_SIZE: {
		RECT rc;
		GetClientRect(main_wnd, &rc);

		int x = 0;
		MoveWindow(child_wnd, x, 0, rc.right - x, 60, TRUE);
		MoveWindow(hge->System_GetState(HGE_HWND), x, 0, rc.right - x, 60, TRUE);
	
		return 0;
				  }
	default:  
        return DefWindowProc(wnd, msg, wParam, lParam);  
    }  
    return 0;  
}  
LRESULT CALLBACK onChildWndMessage(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam) {  
 
		return DefWindowProc(wnd, msg, wParam, lParam);  

}  
/************************************************************************ 
    登记自己的窗口类 
************************************************************************/  
bool registerMyClass() {  
    WNDCLASSEX  wce = {0};  
    wce.cbSize          = sizeof(wce);  
    wce.style           = CS_VREDRAW | CS_HREDRAW;  
    wce.lpfnWndProc     = &onMainWndMessage;  //指明回调函数  
    wce.hInstance       = GetModuleHandle(0);  
    wce.hIcon           = LoadIcon(0, MAKEINTRESOURCE(IDI_WINLOGO));  
    wce.hCursor         = LoadCursor(0, MAKEINTRESOURCE(IDC_ARROW));  
    wce.hbrBackground   = reinterpret_cast<HBRUSH>(COLOR_BTNFACE+1);  
    wce.lpszClassName   = CLASS_NAME; //独一无二的类名  
    wce.hIconSm         = wce.hIcon;  
    return 0!=(RegisterClassEx(&wce)&&(registerChildClass()));  
}  
bool registerChildClass() {  
	WNDCLASSEX  wce = {0};  
	wce.cbSize          = sizeof(wce);  
	wce.style           = CS_VREDRAW | CS_HREDRAW;  
	wce.lpfnWndProc     = &onChildWndMessage;  //指明回调函数  
	wce.hInstance       = GetModuleHandle(0);  
	wce.hIcon           = LoadIcon(0, MAKEINTRESOURCE(IDI_WINLOGO));  
	wce.hCursor         = LoadCursor(0, MAKEINTRESOURCE(IDC_ARROW));  
	wce.hbrBackground   = reinterpret_cast<HBRUSH>(BLACK_BRUSH);  
	wce.lpszClassName   = CHILD_CLASS; //独一无二的类名  
	wce.hIconSm         = wce.hIcon;  
	return 0!=RegisterClassEx(&wce);  
}  

/************************************************************************ 
    创建并显示主窗口 
************************************************************************/  
bool createChildWindow(int cmdShow,HWND hwnd,HINSTANCE hinstance);
bool createMyWindow(int cmdShow,HINSTANCE hinstance) {  
    HWND mainWnd = CreateWindowEx(0, CLASS_NAME, L"Demo", WS_OVERLAPPEDWINDOW,  
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,  
        0, 0, GetModuleHandle(0), 0);  
	main_wnd = mainWnd;
	createChildWindow(cmdShow,mainWnd,hinstance);
    if (0!=mainWnd) {  
        ShowWindow(mainWnd, cmdShow);  
        UpdateWindow(mainWnd);  
        return true;  
    } else {  
        return false;  
    }  
}  
bool createChildWindow(int cmdShow,HWND hwnd,HINSTANCE hinstance) {  
	HWND child =  CreateWindowW(CHILD_CLASS, 0,
		WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT |
		ES_AUTOVSCROLL | ES_AUTOHSCROLL,
		0, 0, 0, 0, hwnd, 0,hinstance , 0);
	child_wnd = child;
	if (0!=child) {  
		ShowWindow(child, cmdShow);  
		UpdateWindow(child);  
		return true;  
	} else {  
		return false;  
	}  
}  

/************************************************************************ 
    消息循环 
************************************************************************/  
int messageLoop() {  
    MSG msg;  
    while (GetMessage(&msg, 0, 0, 0)) {  
        TranslateMessage(&msg);  
        DispatchMessage(&msg);  

    }  
    return static_cast<int>(msg.wParam);  
}  
 bool FrameFunc(){
	 return false;
 } 
 hgeQuad quad; // 定义四边形
 
 bool RenderFunc()
 {

	 return false;
 }
 DWORD WINAPI hge_start_func(void* wParam)
 {
	 if(hge->System_GetState(HGE_HWND)){
		 hge->System_Start();
	 }

	 return 0;

 }

/************************************************************************ 
    WinMain，程序入口 
************************************************************************/  
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE , char *, int cmdShow) {  

	hge = hgeCreate(HGE_VERSION);
	if (registerMyClass() && createMyWindow(cmdShow,hinstance)) {  
        
		
		hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
		hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
			hge->System_SetState(HGE_WINDOWED,true);
		hge->System_SetState(HGE_HWNDPARENT, child_wnd);
		hge->System_SetState(HGE_HIDEMOUSE, false);
		hge->System_SetState(HGE_DONTSUSPEND,false);
		hge->System_Initiate();
		//HANDLE hThread;
//		DWORD dwThread;
		//hThread = CreateThread(NULL,0,hge_start_func,NULL,0,&dwThread);
		
		// messageLoop();
		// CloseHandle(hThread);
		// return 0;
		for(;;)
		{
			MSG msg;
			if(hge->System_GetState(HGE_HWND))
				hge->System_Start();

			if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
			{ 
				if(msg.message == WM_QUIT) break;
				DispatchMessage(&msg);
			}
		}
		
	}
	return 0;
}   